#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isEvenOddTree(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        int layers = 0;
        while (q.size())
        {
            int n = q.size();
            while (n--)
            {
                TreeNode* tmp = q.front();
                if (tmp->val % 2 == layers % 2)
                    return false;
                q.pop();
                TreeNode* next;
                if (q.size())
                    next = q.front();
                if (layers % 2)
                {
                    if (n && tmp->val <= next->val)
                        return false;
                }
                else
                {
                    if (n && tmp->val >= next->val)
                        return false;
                }
                if (tmp->left)
                    q.push(tmp->left);
                if (tmp->right)
                    q.push(tmp->right);
            }
            layers++;
        }
        return true;
    }
};